using System;
using com.yoozoo.gta.Extension;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using Yoozoo.Core.Common;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.PlayerController;
using Yoozoo.Mars.Got;

namespace com.yoozoo.gta.Art.RpgScene
{
    public class WaterScreenSpaceRefraction : MonoBehaviour
    {
        [SerializeField] [Header("玩家面向水体时打开ColorTexture（优化性能）")]private bool enablefaceState = true;
        [SerializeField] private float faceStateThreshold = -0.5f;
        [SerializeField] private LayerMask _layerMask;
        private readonly string _thisPlayerColliderName = "player_model_mid_RPG_controller";//玩家
        private readonly string _thisCarColliderName = "Car_Root";//开车时候使用的碰撞体
        private readonly string _waterKeyWord = "_CAMERAOPAQUETEXTURE_ON";//shader内屏幕空间折射开关
        
        private UniversalAdditionalCameraData _additionalCamera;

        private void OnTriggerEnter(Collider other)
        {
            if (LibertyAIUtils.PlayerController)
            {
                _additionalCamera = LibertyAIUtils.PlayerController.cameraController.playerCamera.GetUniversalAdditionalCameraData();
            }
        }

        private void OnTriggerStay(Collider thisCollision)
        {
            if (QualityHelper.CurrentAdjusterSetting.quality == QualityEnum.Perfect
                || QualityHelper.CurrentAdjusterSetting.quality == QualityEnum.VeryHigh)
            {
                if (LibertyAIUtils.PlayerController != null && LibertyAIUtils.PlayerController.ControlMode == ControlMode.DriveCar)
                {
                    if (thisCollision.transform.name == _thisCarColliderName)
                    {
                        ChangeState();
                    }
                }
                else
                {
                    if (thisCollision.gameObject.layer ==_layerMask)
                    {
                        ChangeState();
                    }
                }
            }
        }

        private void ChangeState()
        {
            if (QualityHelper.CurrentAdjusterSetting.quality == QualityEnum.Perfect
                || QualityHelper.CurrentAdjusterSetting.quality == QualityEnum.VeryHigh)
            {
                if (enablefaceState)
                {
                    if (_additionalCamera!=null)
                    {
                        //相机是否面向水体
                        float faceState =
                            Vector3.Dot(LibertyAIUtils.PlayerController.cameraController.playerCamera.transform.forward,
                                this.transform.position);
                    
                        if (faceState > faceStateThreshold)
                        {
                            if (_additionalCamera.requiresColorTexture == false)
                            {
                                _additionalCamera.requiresColorTexture = true;
                                Shader.EnableKeyword(_waterKeyWord);
                                Debug.LogWarning( "已开启 <color=red> Color Texture</color> ！" + "进入GTA世界水体范围内,"+"位置:" + this.transform.position+"，父级："+this.transform.parent.name );
                            }
                        }
                        else
                        {
                            if (_additionalCamera.requiresColorTexture == true)
                            {
                                _additionalCamera.requiresColorTexture = false;
                                Shader.DisableKeyword(_waterKeyWord);
                                Debug.LogWarning(   "已关闭 <color=red> Color Texture</color> ！"+ "位于GTA世界水体范围内,"+"位置:" + this.transform.position+"，父级："+this.transform.parent.name);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogWarning("_additionalCamera 相机为空");
                    }
                }
                else
                {
                    if (_additionalCamera != null && _additionalCamera.requiresColorTexture == false)
                    {
                        _additionalCamera.requiresColorTexture = true;
                        Shader.EnableKeyword(_waterKeyWord);
                        Debug.LogWarning( "已开启 <color=red> Color Texture</color> ！" + "进入GTA世界水体范围内,"+"位置:" + this.transform.position+"，父级："+this.transform.parent.name );
                    }
                }
                    
            }
        }

        private void OnTriggerExit(Collider thisCollision)
        {
            if (thisCollision.transform.name == _thisPlayerColliderName || thisCollision.transform.name == _thisCarColliderName)
            {
                if (QualityHelper.CurrentAdjusterSetting.quality == QualityEnum.Perfect
                    || QualityHelper.CurrentAdjusterSetting.quality == QualityEnum.VeryHigh)
                {
                    _additionalCamera = LibertyAIUtils.PlayerController.cameraController.playerCamera.GetUniversalAdditionalCameraData();
                    _additionalCamera.requiresColorTexture = false;
                    Shader.DisableKeyword(_waterKeyWord);
                    _additionalCamera = null;
                    Debug.LogWarning( "已关闭 <color=red> Color Texture</color> ！" + "离开GTA世界水体范围内,"+"位置:" + this.transform.position+"，父级："+ this.transform.parent.name);
                }
            }
        }

        private void OnDisable()
        {
            if (null != _additionalCamera)
            {
                _additionalCamera.requiresColorTexture = false;
                _additionalCamera = null;
            }
        }
    }
}